package com.qyhl.module_activities.utils.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RadioGroup;

/**
 * Created  on 2017/12/26.
 *
 * @author helin
 * 自动换行的RadioGroup
 */

public class FlowRadioGroup extends RadioGroup {

    public FlowRadioGroup(Context context) {
        super(context);
    }

    public FlowRadioGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //总宽度
        int maxWidth = MeasureSpec.getSize(widthMeasureSpec);
        //总高度
        int maxHeight = 0;
        //当前行宽度
        int nowLineWidth = 0;
        //当前行最大高度
        int nowLineHeight = 0;
        measureChildren(widthMeasureSpec, heightMeasureSpec);
        for (int index = 0; index < getChildCount(); index++) {
            final View child = getChildAt(index);
            if (child.getVisibility() != View.GONE) {
                int width = child.getMeasuredWidth();
                int height = child.getMeasuredHeight();

                if ((nowLineWidth + width) > maxWidth) {
                    //折行或者孩子本身宽度就大于总宽度,此时都需要新开一行，高度叠加
                    //重置当前行宽度
                    nowLineWidth = width;
                    //重置当前行高度
                    nowLineHeight = height;
                    //总高度叠加
                    maxHeight += nowLineHeight;
                } else {
                    //不折行
                    //当前行宽度叠加
                    nowLineWidth += width;
                    if (nowLineHeight < height) {
                        //孩子宽度大于当前行高度，更新总高度
                        maxHeight = maxHeight - nowLineHeight + height;
                        //重置当前行高度
                        nowLineHeight = height;
                    }
                }
            }
        }
        setMeasuredDimension(maxWidth, maxHeight);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int maxWidth = r - l;
        //总高度
        int maxHeight = 0;
        //当前行宽度
        int nowLineWidth = 0;
        //当前行最大高度
        int nowLineHeight = 0;
        for (int i = 0; i < getChildCount(); i++) {
            final View child = this.getChildAt(i);
            if (child.getVisibility() != View.GONE) {
                int width = child.getMeasuredWidth();
                int height = child.getMeasuredHeight();

                if ((nowLineWidth + width) > maxWidth) {
                    //折行
                    //重置当前行宽度
                    nowLineWidth = width;
                    //重置当前行高度
                    nowLineHeight = height;
                    //总高度叠加
                    maxHeight += nowLineHeight;
                } else {
                    //不折行
                    //当前行宽度叠加
                    nowLineWidth += width;
                    if (nowLineHeight < height) {
                        //孩子宽度大于当前行高度，更新总高度
                        maxHeight = maxHeight - nowLineHeight + height;
                        //重置当前行高度
                        nowLineHeight = height;
                    }
                }
                child.layout(nowLineWidth - width, maxHeight - height, nowLineWidth, maxHeight);
            }
        }
    }
}
